3、凑算式

题目 凑算式

image-e99e4ece

思路分析

题目错了 应该是1-9的数字

暴力枚举每个数

注意最后判断的时候不能直接做加法 要对公式做个变形

因为只有能整除才合法 不能整除 丢失精度使得答案一样的方案不算

可以写成先全排列 再套到ABCDEFGHI中

image-41015459

懒得写dfs的了 直接写next_permutation

代码实现

暴力枚举

#include<bits/stdc++.h>

using namespace std;

int main()

{

  int a,b,c,d,e,f,g,h,i;

  int cnt=0;

  for(a=1;a<=9;a++)

  {

    for(b=1;b<=9;b++)

    {

      for(c=1;c<=9;c++)

      {

        for(d=1;d<=9;d++)

        {

          for(e=1;e<=9;e++)

          {

            for(f=1;f<=9;f++)

            {

              for(g=1;g<=9;g++)

              {

                for(h=1;h<=9;h++)

                {

                  for(i=1;i<=9;i++)

                  {

                    if(a!=b && a!=c && a!=d && a!=e && a!=f && a!=g && a!=h && a!=i

                      &&

                      b!=c && b!=d && b!=e && b!=f && b!=g && b!=h && b!=i

                      &&

                      c!=d && c!=e && c!=f && c!=g && c!=h && c!=i

                      &&

                      d!=e && d!=f && d!=g && d!=h && d!=i

                      &&

                      e!=f && e!=g && e!=h && e!=i

                      &&

                      f!=g && f!=h && f!=i

                      &&

                      g!=h && g!=i

                      &&

                      h!=i){

                        int m=g*100+h*10+i;

						int n=d*100+e*10+f;

						if((b*n+c*m)/(c*n)==10-a&&(b*n+c*m)%(c*n)==0){

                          cnt++;

                        }

                    }

                  }

                }

              }

            }

          }

        }

      }

    }

  }

  cout<<cnt;

  return 0;

}

全排列

#include<bits/stdc++.h>

using namespace std;

vector<int> a={1,2,3,4,5,6,7,8,9};

int main()

{

  int res=0;

  do{

    int

    int two=a[1];

    int three=a[2];

    int four=a[3]*100+a[4]*10+a[5];

    int five=a[6]*100+a[7]*10+a[8];

    if((one*three*five)+(two*five)+(four*three)==10*three*five)

      res++;

  }while(next_permutation(a.begin(),a.end()));

  cout<<res;

  return 0;

}

同类题型

视频讲解


⬅️ 导弹防御系统 🏠 00-刷题理模型 ➡️ 4、方格填数